package com.xxx.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.demo.dto.GuestBookDto;
import com.xxx.demo.entity.GuestBook;
import com.xxx.demo.service.GuestBookService;
import com.xxx.demo.vo.GuestBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author HT
 * @date 2023/3/28
 * @apiNote
 */
@Controller
@RequestMapping("/manage")
public class GuestBookController {

    @Autowired
    private GuestBookService guestBookService;

    /**
     * 分页查询留言
     * @param pageNo
     * @param pageSize
     * @param guestBook
     * @param model
     * @return
     */
    @RequestMapping("/queryGuestBook")
    public String queryNews(Integer pageNo, Integer pageSize, @ModelAttribute("guestbook")GuestBookDto guestBook, Model model){
        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 3;
        }
        IPage<GuestBookVo> guestBookIPage = guestBookService.selectGuestBookPage(pageNo, pageSize, guestBook);
        List<GuestBookVo> records = guestBookIPage.getRecords();
        if (CollectionUtil.isEmpty(records)) {
            return "/manage/guestbook";
        } else {
            for (GuestBookVo date : records) {
                date.setStrId(date.getMessageId() + "!");
                Date creationTime = date.getCreateTime();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = simpleDateFormat.format(creationTime);
                date.setGuestBookTime(format);
            }
            guestBookIPage.setRecords(records);
        }
        model.addAttribute("guestBookPage", guestBookIPage);
        return "/manage/guestbook";
    }



    /**
     * 新增留言
     * @param guestBook
     * @return
     */
    @RequestMapping("/insertGuestBook")
    @ResponseBody
    public String insertGuestBook(@ModelAttribute GuestBook guestBook){
        int i = guestBookService.saveGuestBook(guestBook);
        System.out.println(guestBook);
        if (i == 0){
            return "0";
        }
        return "1";
    }



    /**
     * 根据Id删除留言（逻辑删除）
     * @param id
     * @return
     */
    @RequestMapping("/deleteGuestBook")
    @ResponseBody
    public String deleteGuestBook(String id){
        id = id.replace("!","");
        System.out.println(id);
        Long aLong = Long.valueOf(id);
        guestBookService.deleteGuestBook(aLong);
        return "1";
    }

    /**
     * 根据id查询留言
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/queryGuestBookById")
    public String queryGuestBookById(Long id,Model model){
        GuestBook guestBook = guestBookService.queryGuestBookById(id);
        model.addAttribute("guestBook",guestBook);
        return "manage/updateGuestBook";
    }



    /**
     * 修改留言
     * @param guestBook
     * @return
     */
    @RequestMapping("/updateGuestBook")
    @ResponseBody
    public String updateGuestBook(@ModelAttribute GuestBook guestBook){
        guestBookService.updateGuestBook(guestBook);
        return "1";
    }

    @RequestMapping("/selectGuestBookPage")
    public String selectGuestBookPage(Integer pageNo, Integer pageSize, @ModelAttribute("guestbook") GuestBookDto guestBook, Model model) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 3;
        }
        IPage<GuestBookVo> guestBookIPage = guestBookService.selectGuestBookPage(pageNo, pageSize, guestBook);
        List<GuestBookVo> records = guestBookIPage.getRecords();
        if (CollectionUtil.isEmpty(records)) {
            return "/manage/guestbook";
        } else {
            for (GuestBookVo date : records) {
                date.setStrId(date.getMessageId() + "!");
                Date creationTime = date.getCreateTime();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = simpleDateFormat.format(creationTime);
                date.setGuestBookTime(format);
            }
            guestBookIPage.setRecords(records);
        }
        model.addAttribute("guestBookPage", guestBookIPage);
        return "/manage/guestbook";
    }

}
